package cn.zs.middleware.infrastructure.adapter.repository;

import cn.zs.middleware.domain.xxx.adapter.repository.IAwardRepository;
import cn.zs.middleware.domain.xxx.model.entity.AwardEntity;
import cn.zs.middleware.infrastructure.dao.IAwardDao;
import cn.zs.middleware.infrastructure.dao.po.Award;
import cn.zs.middleware.types.common.JsonUtil;
import com.alibaba.fastjson2.JSON;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Auther: zs
 * @Date: 2025/05/12/19:18
 */
@Repository
public class AwardRepository implements IAwardRepository {

    @Resource
    private IAwardDao awardDao;
    @Override
    @Cached(name="AwardCacheAll-3:", key = "'all'", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.REMOTE)
    public String queryAwardList() {
        List<Award> awards = awardDao.queryAwardList();

        return JSON.toJSONString(awards);
    }

    @Override
    public AwardEntity queryAwardById(int id) {
        Award award = awardDao.queryAwardById(id);
        return AwardEntity.builder()
                .id(award.getId())
                .awardId(award.getAwardId())
                .awardKey(award.getAwardKey())
                .awardConfig(award.getAwardConfig())
                .awardDesc(award.getAwardDesc())
                .build();
    }

    @Override
    public boolean updateAward(AwardEntity awardEntity) {
        Award award = Award.builder()
                .id(awardEntity.getId())
                .awardId(awardEntity.getAwardId())
                .awardKey(awardEntity.getAwardKey())
                .awardConfig(awardEntity.getAwardConfig())
                .awardDesc(awardEntity.getAwardDesc())
                .build();

              return  awardDao.updateAward(award) > 0 ;

    }
}
